package client;

import java.io.BufferedReader;
import java.io.IOException;

public class ClientCommandInterpreter extends CommandInterpreter {

    private final BufferedReader br;

    public ClientCommandInterpreter(BufferedReader br, ClientProxy cp) {
        super(x -> System.out.println("Wrong command. Enter '/help' for more help"));

        this.br = br;

        CommandHandler syncRequest = args -> {
            System.out.println(args);
            System.out.println(cp.send(args));
        };
        addCommand("/snd", syncRequest);
        addCommand("/sndp", syncRequest);
        addCommand("/reg", syncRequest);
        addCommand("/log", syncRequest);
        addCommand("/hist", syncRequest);
        addCommand("/histp", syncRequest);
        addCommand("/chroom", syncRequest);
        addCommand("/rooms", syncRequest);
        addCommand("/exit", x -> {
            System.out.println("Exit.");
            stop();
            System.exit(0);
        });
        addCommand("/help", x -> showHelp());
    }

    private void showHelp() {
        System.out.println("Available commands:");
        System.out.println("/snd <text> - send text message to common chat");
        System.out.println("/sndp <user> <text> - send private <text> for <user>");
        System.out.println("/reg <user> <password> - try to create new <user> with <password>");
        System.out.println("/log <user> <password> - try to login <user> with <password>");
        System.out.println("/chroom <room> - go to <room> chatroom");
        System.out.println("/rooms - get list of actual rooms");
        System.out.println("/hist <room> - get full history of <room> chat");
        System.out.println("/hist all - get full history of all chat rooms");
        System.out.println("/histp - get all private history");
        System.out.println("/exit - exit from chat client");
        System.out.println("/help - show help");
    }

    @Override
    public String getCommandWithArgs() throws IOException {
        return br.readLine();
    }
}
